package com.exchange.used.web.api;

import com.exchange.used.config.converters.LayuiResult;
import com.exchange.used.config.query.Condition;
import com.exchange.used.config.utils.FileUploadUtil;
import com.exchange.used.service.RestfulServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * post/创建
 * delete/删除
 * put/更新或创建
 * get/查看
 * 由于Tomcat8版本升级service方法中不在支持put和delete，坑爹！！所以不适用
 */

// @Api(value = "接口",description ="restful interface" )
@RestController
@RequestMapping(value = "/restful")
public class RestfulApiController {
    
    @Autowired
    private RestfulServiceI commonService;

    /**
     * 插入或更新 实体类
     * @param dataTable 数据表名称
     * @param datas 待插入或修改的数据
     * @return 返回结果的包装类
     */
    @RequestMapping(value = "/save/{dataTable}", method = RequestMethod.POST)
    @Transactional
    public LayuiResult saveOrUpdateEntity(
            @PathVariable String dataTable,
            @RequestBody List<Map<String, Object>> datas) {
        LayuiResult layuiResult = new LayuiResult();
        try {
            if(!commonService.executeSaveOrUpdate(dataTable, datas)){
                layuiResult.setCode(-1);
                layuiResult.setMsg("操作失败");
            }
        }catch (Exception e){
            layuiResult.setCode(-1);
            layuiResult.setMsg(e.getMessage());
        }
        return layuiResult;
    }

    /**
     * 删除实体类
     * @param dataTable 数据表名
     * @param ids 待删除数据表的主键
     * @return 返回结果的包装类
     */
    @RequestMapping(value = "/delete/{dataTable}", method = RequestMethod.POST)
    @Transactional
    public LayuiResult deleteEntity(
            @PathVariable("dataTable") String dataTable,
            @RequestBody List<Object> ids) {
        LayuiResult result = new LayuiResult();
        try {
            if (!commonService.executeDelete(dataTable, ids)){
                result.setMsg("操作失败");
                result.setCode(-1);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setCode(-1);
        }
        return result;
    }



    /**
     * 查询实体类（多条件、近似、精确、分页 查询 多实体类）
     * @param condition 查询条件
     * @return RESULT:返回的主要数据信息，COUNT为总数据条目数
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @Transactional
    public Map<String, Object> seniorFind(@RequestBody Condition condition) {
        List<Map<String, Object>> list;
        Map<String, Object> results;
        try {
            list = commonService.executeQuery(condition);
            results = commonService.getCount(condition);
            results.put("data", list);
            results.put("code", 0);
            return results;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @Transactional
    public LayuiResult upload(@RequestParam("file") MultipartFile file,
                              HttpServletRequest request) {

        LayuiResult layuiResult = new LayuiResult();
        String contentType = file.getContentType();   //文件类型
        String fileName = file.getOriginalFilename();  //文件名字
        String path = null;
        try {
            path = FileUploadUtil.saveFile(file.getInputStream(), fileName);
            layuiResult.getData().put("path", path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return layuiResult;
    }
}
